local DIY = require "packages.diy_utility.diy_utility"
local LB_U = require "packages.lb_kill_utility.utility"

local skel = fk.CreateSkill {
  name = "lb_zero__disu",
  tags = { LB_U.LeaderSkill }
}

Fk:loadTranslationTable {
  ["lb_zero__disu"] = "涤肃",
  [":lb_zero__disu"] = "<b><a href='lb_leader_skill'><font color='red'>领袖技</font></a></b>，每回合限一次，“维多利亚家政”的角色造成伤害后，其可以将本回合因弃置进入弃牌堆的至多两张牌置入牌堆底，然后其摸等量张牌。",

  ["#lb_zero__disu"] = "涤肃：你可以至多两张牌置入牌堆底，然后摸等量张牌",

  ["$lb_zero__disu1"] = "明白。",
  ["$lb_zero__disu2"] = "清算！",
}

skel:addEffect(fk.Damage, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) and player:usedSkillTimes(skel.name, Player.HistoryTurn) == 0
        and target and LB_U.CompareKingdomWith(target, "lb_zero_weiduoliyajiazheng") then
      local room = player.room
      local logic = room.logic
      local discards = {}
      for _, id in ipairs(DIY.getCenterCards(room)) do
        logic:getEventsByRule(GameEvent.MoveCards, 1, function(e)
          local move = e.data
          for i = #move, 1, -1 do
            local moveInfo = move[i].moveInfo
            for j = #moveInfo, 1, -1 do
              if moveInfo[j].cardId == id then
                if move[i].moveReason == fk.ReasonDiscard then -- 只找最后一次移动
                  table.insert(discards, id)
                end
                return true
              end
            end
          end
        end, nil, Player.HistoryTurn)
      end
      if #discards > 0 then
        event:setCostData(self, { cards = discards})
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local discards = event:getCostData(self).cards
    if #discards > 0 then
      local cards = room:askToGuanxing(player, {
        cards = discards,
        area_names = { "pile_discard", "Bottom" },
        --top_limit = { n, n },
        bottom_limit = { 0, 2 },
        skill_name = skel.name,
        prompt = "#lb_zero__disu",
        skip = true,
      }).bottom
      if #cards > 0 then
        event:setCostData(self, { cards = cards })
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local cards = event:getCostData(self).cards
    player.room:moveCards({
      ids = cards,
      toArea = Card.DrawPile,
      moveReason = fk.ReasonPut,
      drawPilePosition = -1,
      skillName = skel.name,
      proposer = target,
      moveVisible = true,
    })
    player.room:sendLog {
      type = "#PutCardBottom",
      card = cards,
    }
    if target:isAlive() then
      target:drawCards(#cards, skel.name)
    end
  end,
})

skel:addAcquireEffect(function(self, player, is_start)
  player.room:addSkill("#CenterArea")
end)


return skel
